﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
namespace ChuanPlus.DAO
{
    class ProductionDAO
    {
        //
        // save an Production to database, 'stockCard',stock quantity as 1 UnitOfWork
        //
        public static void save(Production Production)// ok
        {
            Session.DefaultSession.BeginTransaction();
            try
            {
                // Luu stockcard (lay Product ra che bien)
                // Tru quantity con lai trong Product
                StockCard st;
                int countIng = Production.ProductionDetails.Count();
                // lay TransactionType 
                XPQuery<TransactionType> transtype = new XPQuery<TransactionType>(Session.DefaultSession);
                var list = from t in transtype
                           where t.TransactionTypeID == 3
                           select t;
                List<TransactionType> arr = new List<TransactionType>(list.AsEnumerable<TransactionType>());
                TransactionType transtypeObj = arr[0];
                ProductionDetails proDetail = null;
                Product p = null;
                Stock stockObj = null;
                long stockCardID = StockCardDAO.GenerateId();
                for (int i = 0; i < countIng; i++)
                {
                    proDetail = Production.ProductionDetails[i];

                    // Tru QtyBalance cua Product & set balance cho stockcard
                    //Khi xuat hàng: tru hàng vào QtyBalance của Product , đồng thời gán balance của Stockcard = QtyBalance
                    p = proDetail.StockID.ID;
                    stockObj = proDetail.StockID;
                    //check lai balance? dung cong thuc ?
                    double balance = stockObj.QtyBalance - proDetail.ActualUsed;
                    stockObj.QtyBalance = balance;
                    stockObj.Save();
                    // Luu dzo Stockcard
                    
                    st = new StockCard(Session.DefaultSession,++stockCardID, stockObj, Production.ProductionDate, 0.0f, proDetail.ActualUsed, balance, transtypeObj, Production.ProductionID.ToString());
                    st.Save();
                }
                ///// nhap kho sp moi
                // Tao Stockcard nhap kho
// check lai balance
                double inbalance = Production.StockID.QtyBalance + Production.TotalUsed - Production.Loss;
                StockCard stockcard = new StockCard(Session.DefaultSession, ++stockCardID, Production.StockID, Production.ProductionDate, Production.TotalUsed - Production.Loss, 0, inbalance, transtypeObj, Production.ProductionID.ToString());
                stockcard.Save();
                // Them QtyBalance trong Stock
                Production.StockID.QtyBalance = inbalance;
                Production.StockID.Save();
                // cong redInv trong Product
                Production.StockID.ID.RedInvQty += (Production.TotalUsed - Production.Loss);             
                Production.StockID.ID.Save();
                Production.Status = "P";
                Production.Save();

                Session.DefaultSession.CommitTransaction();
                
            }
            catch (Exception ex)
            {
                Session.DefaultSession.RollbackTransaction();
                throw new Exception(ex.Message);
            }
            
        }
        public static List<Production> GetProductions(string status)
        {
            try
            {
                XPQuery<Production> production = new XPQuery<Production>(Session.DefaultSession);
                var list = from p in production
                           where p.Status == status
                           select p;
                return new List<Production>(list.AsEnumerable<Production>());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        //
        // update an Production to database, 'stockCard', stock quantity as 1 UnitOfWork
        // optional method
        public static bool update(Production Production)
        {
            return true;
        }
        public static List<Production> GetAllProductions() //ok
        {
            Session session = new Session();
            XPQuery<Production> Productions = new XPQuery<Production>(session);
            var list =  from prod in Productions                                         
                       select prod;
            List<Production> arr = new List<Production>(list.AsEnumerable<Production>());
            try
            {
                return arr;
            }
            catch (Exception e) { return null; }
        }
        //
        //return list of Production by ProductionDate base on day
        // order by ProductionDate DESC
        public static List<Production> GetProductionByDay(DateTime d) //ok
        {
            Session session = new Session();
            XPQuery<Production> productions = new XPQuery<Production>(session);
            var list = from p in productions
                       where p.ProductionDate == d
                       orderby p.ProductionDate descending
                       select p;
            List<Production> arr = new List<Production>(list.AsEnumerable<Production>());
            try
            {
                return arr;
            }
            catch (Exception e) { return null; }
        }
        //
        //return list of Production by ProductionDate base on fromDate, toDate
        // order by ProductionDate DESC
        public static List<Production> GetProductionByMonth(DateTime fromDate, DateTime toDate) //ok
        {
            XPQuery<Production> productions = new XPQuery<Production>(Session.DefaultSession);
            var list = from p in productions
                       where (p.ProductionDate >= fromDate) && (p.ProductionDate <= toDate)
                       select p;
            List<Production> arr = new List<Production>(list.AsEnumerable<Production>());
            try
            {
                return arr;
            }
            catch (Exception e) { return null; }
        }
        //
        //return list of Production by ProductionDate base on Production Number
        // order by ProductionDate DESC
        public static Production GetProductionByNo(int ProductionNo) //ok
        {
            XPQuery<Production> productions = new XPQuery<Production>(Session.DefaultSession);
            var list = from p in productions
                       where p.ProductionID == ProductionNo
                       orderby p.ProductionDate descending
                       select p;
            List<Production> arr = new List<Production>(list.AsEnumerable<Production>());
            try
            {
                return arr[0];
            }
            catch (Exception e) { return null; }
        }        
    }
    
}
